package com.gongyinlian.personalprofilewebsiteserver.controller;

import com.gongyinlian.personalprofilewebsiteserver.common.result.Result;
import com.gongyinlian.personalprofilewebsiteserver.entity.SysUser;
import com.gongyinlian.personalprofilewebsiteserver.mapper.SysUserMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试控制器
 * 
 * @author personal-profile-website
 * @date 2024-10-27
 */
@Tag(name = "测试接口", description = "用于测试项目配置是否正常")
@RestController
@RequestMapping("/test")
public class TestController {
    
    @Autowired(required = false)
    private SysUserMapper sysUserMapper;
    
    /**
     * Hello 测试接口
     */
    @Operation(summary = "Hello 测试", description = "测试项目是否正常启动")
    @GetMapping("/hello")
    public Result<Map<String, Object>> hello() {
        Map<String, Object> data = new HashMap<>();
        data.put("message", "Hello from Personal Profile Website API!");
        data.put("timestamp", LocalDateTime.now());
        data.put("version", "2.0.0");
        data.put("status", "running");
        
        return Result.success("系统运行正常", data);
    }
    
    /**
     * 数据库连接测试接口
     */
    @Operation(summary = "数据库连接测试", description = "测试数据库连接是否正常（需要先执行 SQL 脚本）")
    @GetMapping("/db")
    public Result<Map<String, Object>> testDatabase() {
        Map<String, Object> data = new HashMap<>();
        
        try {
            if (sysUserMapper == null) {
                data.put("status", "error");
                data.put("message", "SysUserMapper 未初始化");
                return Result.error("数据库连接失败");
            }
            
            // 查询用户数量
            Long count = sysUserMapper.selectCount(null);
            data.put("userCount", count);
            data.put("status", "success");
            data.put("message", "数据库连接正常");
            
            // 如果有数据，查询第一个用户
            if (count > 0) {
                List<SysUser> users = sysUserMapper.selectList(null);
                data.put("sampleUser", users.get(0).getUsername());
            }
            
            return Result.success("数据库连接成功", data);
            
        } catch (Exception e) {
            data.put("status", "error");
            data.put("message", e.getMessage());
            data.put("tip", "请先执行数据库 SQL 脚本创建数据库和表");
            return Result.error(500, "数据库连接异常：" + e.getMessage());
        }
    }
    
    /**
     * 系统信息接口
     */
    @Operation(summary = "系统信息", description = "获取系统基本信息")
    @GetMapping("/info")
    public Result<Map<String, Object>> systemInfo() {
        Map<String, Object> data = new HashMap<>();
        data.put("projectName", "Personal Profile Website");
        data.put("version", "2.0.0");
        data.put("springBootVersion", "3.5.7");
        data.put("javaVersion", System.getProperty("java.version"));
        data.put("os", System.getProperty("os.name"));
        data.put("author", "personal-profile-website");
        data.put("description", "个人作品集系统后端 API");
        
        return Result.success(data);
    }
}

